using AspectCore.DynamicProxy;
using Vin.Extension.Aop.Attributes;
using Vin.Extension.Framework.Models;
using Vin.Extension.Framework.Tools;
using Vin.Extension.Redis.Cache;
using Vin.Tool.Core.AssertCategory;
using Vin.Tool.Domain.ExceptionModel;

namespace Vin.Extension.Framework.Aop;

public class VinJwtRepeatAttribute(string repeatKey = "") : VinBaseInterceptorAttribute
{
    public string RepeatKey { get; set; } = repeatKey;

    public string? TokenPrefix { get; set; }

    public string ErrorMsg { get; set; } = "系统繁忙，请稍后再试";

    public override async Task Invoke(AspectContext context, AspectDelegate next)
    {
        var model = VinTokenModelTool.GetTokenModel(TokenPrefix);
        if (model != null)
        {
            if (await VinJwtRepeatTool.ExistsAsync(RepeatKey, model.UserId!))
                throw new BusinessException(ErrorMsg);
        }
    }
}